package bhbt.controller;


import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Admin;
import bhbt.model.Member;
import bhbt.model.log.LogOperAdmin;
import bhbt.service.AdminService;
import bhbt.service.log.LogOperAdminService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;
import bhbt.utils.SysConst;






/**
 * 登录
 */
@Controller

public class LoginAdminController extends BaseController {

	@Resource
	private AdminService adminService;
	@Resource
	private LogOperAdminService logOperAdminService;
	
	@RequestMapping("loginAdmin.shtml")
	public String submit(ModelMap model, HttpSession session, HttpServletRequest request) throws Exception{
		Map formMap = getParameterMap(request);
		String onePassword =  MapUtil.getString(formMap, "onePassword"); 
		String loginName =  MapUtil.getString(formMap, "loginName"); 
		Admin admin = verifyAdmin(loginName);
		if(null != admin){
			//密码加密
			onePassword = CryptUtil.getInstance().encrypt2String(onePassword,CryptUtil.keyStr);
			
			if(onePassword.equals(admin.getOnePassword())){
				
				
				
				Map map2 = new HashMap();
				 
	        	map2.put("id",admin.getId());
	        	
	        	map2.put("lastTime",admin.getLoginTime());
	        	map2.put("loginTime",new Date());
	        	
	        	if(admin.getLoginNum()==null){
					
					map2.put("loginNum",1);
				}else{
					 
					map2.put("diyUpdate","loginNum=loginNum+"+1);
				}
	        	
				adminService.update(map2);
				
				admin = this.adminService.getByKey(admin.getId());
				session.setAttribute(SysConst.MG, admin);
				
				LogOperAdmin log=new LogOperAdmin();
				log.setType(10);				//操作类别(10系统登录)
				log.setAdminId(admin.getId());
				log.setTitle("系统登录");
				
				String operIp=request.getRemoteHost();
				log.setOperIp(operIp);
				
				log.setCommitDate(new Date());
				log.setContext("登录成功");
				logOperAdminService.saveOrUpdate(log); 
				
				return "redirect:admin/applicationAdmin.shtml";
			}
			else{
				
				admin.setErrorNum(admin.getErrorNum()+1);
				admin.setErrorDate(new Date());
				
				Map map2 = new HashMap();
	        	map2.put("id",admin.getId());
	        	map2.put("errorDate",new Date());
	        	map2.put("diyUpdate","errorNum=errorNum+"+1);
	        	
	        	
				
				adminService.update(map2);
				
				LogOperAdmin log=new LogOperAdmin();
				log.setType(10);				//操作类别(10系统登录)
				log.setAdminId(admin.getId());
				log.setTitle("系统登录");
				
				String operIp=request.getRemoteHost();
				log.setOperIp(operIp);
				
				log.setCommitDate(new Date());
				log.setContext("登录失败");
				logOperAdminService.saveOrUpdate(log); 
				
				
				
				
				setMessage(model,"用户名或密码不正确!");
			}
			
			
			
		}else{
			setMessage(model,"用户不存在!");
		}
		return "loginMg";
	}

	
	/**
	 * 验证管理员登陆
	 * @param member
	 * @return
	 * @throws Exception
	 */
	private Admin verifyAdmin(String loginName) throws Exception{
		//String pwd = admin.getOnePassword();
		// 前台超级用户
		
		Admin admin = adminService.executAdmin(loginName);
		
		return admin;
	}

	@RequestMapping("mgSuccess.shtml")
	public String success() {
		Admin admin = ContextUtil.getAdmin();
		if(admin != null){
			return "admin/applicationAdmin";
		}
		return "loginAdmin";
	}

	/***
	 * 前台退出
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("mgLogoff.shtml")
	public String logoff(HttpServletRequest request) throws Exception{
		removeSession(request.getSession());
		return "loginAdmin";
	}

	private void removeSession(HttpSession session) {
		Enumeration sessionNames = session.getAttributeNames();
		String sessionName = "";
		while (sessionNames.hasMoreElements()) {
			sessionName = sessionNames.nextElement().toString();
			session.removeAttribute(sessionName);
		}
		session.invalidate();
	}


}
